package com.tucc.hello.leetcode.no1_400.no1_200.no41_60.no48;

import cn.hutool.json.JSONUtil;

import java.util.Arrays;
import java.util.List;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[][] nums = solution.readData(TEST_DATA);
        solution.printArr(nums);
        solution.rotate(nums);
        solution.printArr(nums);
    }

    public void rotate(int[][] matrix) {
        int len = matrix.length;
        for(int i = 0;i < len / 2;i ++){
            int[] temp = matrix[i];
            matrix[i] = matrix[len - 1 - i];
            matrix[len - 1 -i] = temp;
        }
        for(int i = 0; i < len; i++) {
            for (int j = i; j < len; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }

    }

    public void getRotateIndex(int[] indexes) {

    }

    public int[][] readData(String data) {
        List<String> stringList = JSONUtil.toList(data, String.class);
        int[][] chars = new int[stringList.size()][];
        for (int i = 0; i < chars.length; i++) {
            String string = stringList.get(i);
            string = string.replaceAll("[\\[\\]]", "");
            String[] parts = string.split(",");
            chars[i] = Arrays.stream(parts).mapToInt(Integer::valueOf).toArray();
        }
        return chars;
    }

    private void printArr(int[][] arr) {
        System.out.println("===================");
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println("===================");
    }

    public static String TEST_DATA = "[[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]";


}
